# -*- Python -*-

# Configuration file for the 'lit' test runner.

import subprocess
import re

# name: The name of this test suite.
config.name = 'libbcc'

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.ll']

# testFormat: The test format to use to interpret tests.
import lit.formats
config.test_format = lit.formats.ShTest()

ANDROID_HOST_OUT = os.getenv("ANDROID_HOST_OUT")
ANDROID_PRODUCT_OUT = os.getenv("ANDROID_PRODUCT_OUT")

if not ANDROID_HOST_OUT or not ANDROID_PRODUCT_OUT:
    import sys
    sys.exit(1)

# test_source_root: The path where tests are located (default is the test suite
# root).
config.test_source_root = None
config.test_exec_root = os.path.join(ANDROID_HOST_OUT, 'tests', 'libbcc')

tools_dir = os.pathsep.join([os.path.join(ANDROID_HOST_OUT, 'bin'),
                             os.path.join(ANDROID_HOST_OUT, 'lib64'),
                             os.path.join(ANDROID_PRODUCT_OUT, 'system/lib')])

# Based on LLVM's lit.cfg: "For each occurrence of an llvm tool name
# as its own word, replace it with the full path to the build directory
# holding that tool."
for pattern in [r"\bFileCheck\b",
                r"\bllvm-rs-as\b",
                r"\bbcinfo\b",
                r"\bopt\b",
                r"\blibbcc.so\b",
                r"\bllvm-objdump\b",
                r"\bbcc\b",
                r"\blibclcore.bc\b"]:
    tool_match = re.match(r"^(\\)?((\| )?)\W+b([\.0-9A-Za-z-_]+)\\b\W*$",
                          pattern)
    tool_pipe = tool_match.group(2)
    tool_name = tool_match.group(4)
    import lit.util
    tool_path = lit.util.which(tool_name, tools_dir)
    if not tool_path:
        lit_config.note("Did not find " + tool_name + " in " + tools_dir)
        tool_path = os.path.join(tools_dir, tool_name)
    config.substitutions.append((pattern, tool_pipe + tool_path))

# The following substitution is needed for the versioninfo tests
llvm_version_string = subprocess.check_output([
    lit.util.which('llvm-config', tools_dir),
    '--version'
]).strip()
config.substitutions.append(('%LLVM_VERSION_STRING', llvm_version_string))
